/*
 * @LastEditors: 七七
 * @Date: 2023-01-05 14:28:16
 */
import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {

  state = { mouse: false } // 标识鼠标移入移出

  // 鼠标移入、移出的回调
  handleMouse = (flag) => {
    // this.handleMouse传值必须使用return函数，确保返回值false,true的是函数
    return () => {
      this.setState({ mouse: flag });
    }
  }

  // 勾选、取消勾选某一个todo的回调
  handleCheck = (id) => {
    return (event) => {
      this.props.updateTodo(id, event.target.checked)
    }
  }

  // 删除某一个todo的回调
  handleDelete = (id) => {
    // 一定要写window
    if (window.confirm('确定删除吗?')) {
      this.props.deleteTodo(id)
    }
  }

  render() {
    const { id, name, done } = this.props
    const { mouse } = this.state
    return (
      <li style={{ backgroundColor: mouse ? '#ddd' : 'white' }} onMouseLeave={this.handleMouse(false)} onMouseEnter={this.handleMouse(true)}>
        <label>
          {/* checked不能更改勾选内容，需要写onChange, defaultChecked可以更改勾选内容 */}
          <input type="checkbox" checked={done} onChange={this.handleCheck(id)} />
          <span>{name}</span>
        </label>
        <button onClick={() => this.handleDelete(id)} className="btn btn-danger" style={{ display: mouse ? 'block' : 'none' }}>删除</button>
      </li>
    )
  }
}